package cn.client.ui.view.main.user.avoidancePain

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.client.data.remoteData.bean.AvoidancePainBean
import cn.client.data.remoteData.bean.AvoidancePainReq
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.remoteData.repository.AvoidancePainRepository
import cn.client.ui.view.main.user.UserInfoViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AvoidancePainViewModel @Inject constructor(
    private val repository: AvoidancePainRepository
) : ViewModel() {

    // 事件管道
    private val _pageEvents = Channel<PageEvent>(Channel.BUFFERED)

    // 可观察事件数据流
    val pageEvents = _pageEvents.receiveAsFlow()

    private val _avoidancePainInfo = MutableStateFlow<AvoidancePainBean?>(null)
    val avoidancePainInfo: StateFlow<AvoidancePainBean?> = _avoidancePainInfo

    private val _avoidancePainList = MutableStateFlow<List<AvoidancePainBean>>(emptyList())
    val avoidancePainList: StateFlow<List<AvoidancePainBean>> = _avoidancePainList


    // 收集意图
    fun dispatch(intent: PageIntent) {
        when (intent) {
            is PageIntent.Add -> add(intent.req)
            is PageIntent.Delete -> delete(intent.id)
            is PageIntent.SelectInfo -> selectInfo(intent.id)
            is PageIntent.SelectList -> selectList(intent.req)
            else -> Unit
        }
    }

    private fun add(req: AvoidancePainReq) {
        viewModelScope.launch {
            try {
                val res = repository.addAvoidancePain(req)
                if (res.code == 200) {
                    _pageEvents.send(PageEvent.Success(res.msg))
                } else {
                    _pageEvents.send(PageEvent.Error(res.msg))
                }
            } catch (e: Exception) {
                val errors = PageEvent.Error(e.localizedMessage ?: "UnKnown Error")
                Log.d("AvoidancePainViewModel", "add 方法发生异常：$e")
                _pageEvents.send(errors)
            }
        }
    }

    private fun selectList(req: PageSelectByUid) {
        viewModelScope.launch {
            try {
                val res = repository.selectListAvoidancePain(req)
                if (res.code == 200) {
                    _avoidancePainList.value = res.data!!
                } else {
                    _pageEvents.send(PageEvent.Error(res.msg))
                }
            } catch (e: Exception) {
                val errors = PageEvent.Error(e.localizedMessage ?: "UnKnown Error")
                Log.d("AvoidancePainViewModel", "add 方法发生异常：$e")
                _pageEvents.send(errors)
            }
        }
    }

    private fun selectInfo(id: Int) {
        viewModelScope.launch {
            try {
                val res = repository.selectInfoAvoidancePain(id)
                if (res.code == 200) {
                    _avoidancePainInfo.value = res.data
                } else {
                    _pageEvents.send(PageEvent.Error(res.msg))
                }
            } catch (e: Exception) {
                val errors = PageEvent.Error(e.localizedMessage ?: "UnKnown Error")
                Log.d("AvoidancePainViewModel", "add 方法发生异常：$e")
                _pageEvents.send(errors)
            }
        }
    }

    private fun delete(id: Int) {
        viewModelScope.launch {
            try {
                val res = repository.deleteAvoidancePain(id)
                if (res.code == 200) {
                    _pageEvents.send(PageEvent.Success(res.msg))
                } else {
                    _pageEvents.send(PageEvent.Error(res.msg))
                }
            } catch (e: Exception) {
                val errors = PageEvent.Error(e.localizedMessage ?: "UnKnown Error")
                Log.d("AvoidancePainViewModel", "add 方法发生异常：$e")
                _pageEvents.send(errors)
            }
        }
    }

    // 意图集
    sealed class PageIntent {
        data class Add(val req: AvoidancePainReq) : PageIntent()
        data class SelectList(val req: PageSelectByUid) : PageIntent()
        data class SelectInfo(val id: Int) : PageIntent()
        data class Delete(val id: Int) : PageIntent()
    }

    // 事件集
    sealed class PageEvent {
        data class Success(val success: String) : PageEvent()
        data class Error(val error: String) : PageEvent()   // 错误信息
    }
}